package com.valuegroup.smarttodo.ui.programme.goals;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.snackbar.Snackbar;
import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.data.vo.TodoVo;
import com.valuegroup.smarttodo.plugins.recyclerview.ItemTouchHelperAdapter;
import com.valuegroup.smarttodo.plugins.recyclerview.ItemTouchHelperViewHolder;
import com.valuegroup.smarttodo.util.Typefaces;

import java.util.Collections;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 习惯清单列表
 */
public class GoalsEventAdapter extends RecyclerView.Adapter implements ItemTouchHelperAdapter {

    private static final String TAG = GoalsEventAdapter.class.getSimpleName();

    private final Context context;
    private final View view;
    private List<TodoVo> itemList;

    public GoalsEventAdapter(Context context, View view, List<TodoVo> itemList) {
        this.context = context;
        this.view = view;
        this.itemList = itemList;
    }

    @Override
    public void onItemDismiss(final int position, boolean isLeftSlide) {
        System.out.println("onItemDismiss:" + position);

        if (getItemViewType(position) != TYPE_CONTENT) {
            return;
        }

        final int listPosition = position - START_COUNT;

        final TodoVo item = new TodoVo();
        item.setTitle(itemList.get(listPosition).getTitle());

        notifyItemRemoved(position);
        itemList.remove(listPosition);
        notifyItemRangeChanged(0, getContentSize());

        final Snackbar snackbar = Snackbar
                .make(view, context.getResources().getString(R.string.item_deleted), Snackbar.LENGTH_LONG)
                .setActionTextColor(Color.WHITE)
                .setAction(context.getResources().getString(R.string.item_undo), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        itemList.add(listPosition, item);
                        notifyItemInserted(position);
                    }
                });

        View snackBarView = snackbar.getView();
        snackBarView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
        TextView tvSnack = snackBarView.findViewById(R.id.snackbar_text);
        TextView tvSnackAction = snackbar.getView().findViewById(R.id.snackbar_action);
        tvSnack.setTextColor(Color.WHITE);
        tvSnack.setTypeface(Typefaces.getRobotoMedium(context));
        tvSnackAction.setTypeface(Typefaces.getRobotoMedium(context));
        snackbar.show();

        Runnable runnableUndo = new Runnable() {

            @Override
            public void run() {
                snackbar.dismiss();
            }
        };
        Handler handlerUndo = new Handler();
        handlerUndo.postDelayed(runnableUndo, 2500);
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        System.out.println("fromPosition:" + fromPosition + "  toPosition:" + toPosition);

        if (getItemViewType(fromPosition) != TYPE_CONTENT) {
            return;
        }

        if (fromPosition < toPosition) {
            for (int i = fromPosition - START_COUNT; i < toPosition - START_COUNT; i++) {
                Collections.swap(itemList, i, i + 1);
            }
        } else {
            for (int i = fromPosition - START_COUNT; i > toPosition - START_COUNT; i--) {
                Collections.swap(itemList, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    public void addItem(int position, TodoVo item) {
        itemList.add(position, item);

        // 因添加了头尾布局，所以需要加上顶部
        notifyItemInserted(position + START_COUNT);
    }

    @Override
    public int getItemCount() {
        return itemList.size() + START_COUNT + END_COUNT;
    }

    public int getContentSize() {
        return itemList.size();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        if (viewType == TYPE_HEAD) {
            return new HeadHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_default_header, viewGroup, false));
        } else if (viewType == TYPE_CONTENT) {
            return new ItemViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_goals_event, viewGroup, false));
        } else {
            return new ButtomHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_default_bottom, viewGroup, false));
        }

    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            ItemViewHolder v = (ItemViewHolder) holder;
            v.initData(itemList.get(position - START_COUNT));
        }
    }

    public final static int START_COUNT = 1;
    public final static int END_COUNT = 1;

    public final static int TYPE_HEAD = 0;
    public final static int TYPE_CONTENT = 1;
    public final static int TYPE_BUTTOM = 2;

    @Override
    public int getItemViewType(int position) {
        int contentSize = getContentSize();
        if (position == 0) {
            return TYPE_HEAD;
        } else if (position == START_COUNT + contentSize) {
            return TYPE_BUTTOM;
        } else {
            return TYPE_CONTENT;
        }
    }

    // 习惯项
    public class ItemViewHolder extends RecyclerView.ViewHolder implements
            ItemTouchHelperViewHolder, View.OnClickListener {

        @BindView(R.id.layoutRoot)
        LinearLayout itemRoot;
        @BindView(R.id.tagEmoji)
        ImageView imgEmoji;
        @BindView(R.id.btnOperation)
        ImageView btnOperation;
        @BindView(R.id.labTitle)
        TextView labTitle;
        @BindView(R.id.labTime)
        TextView labTime;

        private TodoVo data;

        public ItemViewHolder(final View v) {
            super(v);
            ButterKnife.bind(this, v);

            btnOperation.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btnOperation:
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onItemSelected(Context context) {
            itemRoot.setBackgroundColor(ContextCompat.getColor(context, R.color.backgroundDefault));
            //itemText.setTextColor(ContextCompat.getColor(context, R.color.white));
            //ivReorder.setColorFilter(ContextCompat.getColor(context, R.color.white), PorterDuff.Mode.SRC_IN);
        }

        @Override
        public void onItemClear(Context context) {
            itemRoot.setBackgroundColor(ContextCompat.getColor(context, R.color.backgroundDefault));
            //ivReorder.setColorFilter(ContextCompat.getColor(context, R.color.textlight), PorterDuff.Mode.SRC_IN);
            //itemText.setTextColor(ContextCompat.getColor(context, R.color.textDefault));
        }

        public void initData(TodoVo data) {
            this.data = data;
            String subTime = String.format(context.getResources().getString(R.string.lab_minute), data.getConsuming());
            labTitle.setText(data.getTitle());
            labTime.setText(subTime);
            //imgEmoji.setImageResource(data.getEmoji());
        }
    }

    // 头部内容
    public class HeadHolder extends RecyclerView.ViewHolder {
        public HeadHolder(View itemView) {
            super(itemView);
        }
    }

    // 尾部内容
    public class ButtomHolder extends RecyclerView.ViewHolder {
        public ButtomHolder(View itemView) {
            super(itemView);
        }
    }
}
